package com.junmo.oauth.utils;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;


public class ResponseUtil {
    public static void writeMsg(HttpServletResponse response,int status, Object obj){
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(status);
        try {
            response.getOutputStream().write(JSONObject.toJSONString(obj).getBytes(StandardCharsets.UTF_8));
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                response.getOutputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void writeMsg(HttpServletResponse response,Object obj){
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(200);
        try {
            response.getOutputStream().write(JSONObject.toJSONString(obj).getBytes(StandardCharsets.UTF_8));
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                response.getOutputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
